package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.EnrollInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.SchoolNewStudent;
import com.xmy.cultivate.entity.response.EnrollRanking;
import com.xmy.cultivate.entity.views.EnrollDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 招生数据 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2023-02-10
 */
public interface EnrollInfoMapper extends BaseMapper<EnrollInfo> {

    /**
     * 获取成交数
     * @param wrapper
     * @return
     */
    @Select("select IFNULL(SUM(deal_num),0) as deal_num from enroll_info ${ew.customSqlSegment}")
    public BigDecimal getDealNum(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 获取成交数
     * @param wrapper
     * @return
     */
    @Select("select IFNULL(SUM(push_money),0) as deal_num from enroll_info ${ew.customSqlSegment}")
    public BigDecimal getDealMoney(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("SELECT od.school_id, COUNT(DISTINCT od.student_id, od.subjects_id) AS total_count\n" +
            "FROM order_detail as od\n" +
            "JOIN enroll_info as ei ON ei.order_id = od.order_id " +
            "${ew.customSqlSegment}")
    public List<SchoolNewStudent> schoolNewData(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 招生核对率、详情
     */
    @Select("select *,count(id) AS peopleTotal,SUM(IF(check_type = 1,1,0)) AS verifiedNum,SUM(IF(check_type = 0,1,0)) AS notVerifiedNum from (select t8.school_id, t1.id,t1.teacher_id, (t1.push_money * t8.grant_num) AS push_money, t1.type, t1.is_true,t1.is_return, t1.is_grant, t1.check_type, t1.deleted, t1.grant_time, t1.subjects_id, t1.student_id, t1.order_id, t1.grant_date, t1.add_date,t2.year_part,t2.quarter_num,t3.year_class_id,t8.is_grant as school_is_grant,t8.grant_num from enroll_info_grant_school AS t8 INNER JOIN enroll_info AS t1 ON t8.order_id = t1.order_id INNER JOIN order_apply as t2 on t1.order_id = t2.id and t2.status<>2 INNER JOIN order_detail as t3 on t2.id = t3.order_id) as t ${ew.customSqlSegment}")
    public abstract IPage<EnrollDetail> getParticularsRate(IPage<?> page, @Param(Constants.WRAPPER) Wrapper wrapper);


}
